import type { TableColumn, FormField } from '@/components'

export const fields: FormField[] = [
  {
    key: 'backupName',
    label: '备份文件',
    tag: 'ElInput'
  },
  {
    key: 'backupTime',
    label: '备份时间',
    tag: 'el-date-picker',
    attrs: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD'
    }
  },
  {
    key: 'backupType',
    label: '备份类型',
    tag: 'MSelect',
    attrs: {
      options: [
        {
          label: '手动备份',
          value: 1
        },
        {
          label: '自动备份',
          value: 0
        }
      ]
    }
  }
]

export const columns: TableColumn[] = [
  {
    key: 'backupName',
    label: '备份文件'
  },
  {
    key: 'backupTime',
    label: '备份时间',
    customRender: (e) => format(e)
  },
  {
    key: 'backupSize',
    label: '备份大小'
  },
  {
    key: 'backupType',
    label: '备份类型',
    customRender: (e) => (e == 1 ? '手动备份' : '自动备份')
  },
  {
    key: 'createBy',
    label: '操作人'
  },
  {
    key: 'actions',
    label: '操作',
    slot: true,
    fixed: 'right'
  }
]

export const dialogFields: FormField[] = [
  {
    key: 'backupEnum',
    label: '备份间隔时间',
    tag: 'MSelect',
    attrs: {
      options: [
        {
          label: '每天',
          value: 'DAY'
        },
        {
          label: '每周',
          value: 'MONTH'
        },
        {
          label: '每月',
          value: 'WEEK'
        },
        {
          label: '每年',
          value: 'YEAR'
        }
      ]
    },
    validateRules: [
      {
        required: true,
        message: '请输入',
        trigger: 'change'
      }
    ]
  },
  {
    key: 'time',
    label: '备份文件保存时间',
    tag: 'el-time-picker',
    attrs: {
      format: 'HH:mm',
      valueFormat: 'HH:mm'
    },
    validateRules: [
      {
        required: true,
        message: '请输入',
        trigger: 'change'
      }
    ]
  }
]

//格式化时间
const format = (timestamp) => {
  const date = new Date(timestamp)
  const year = date.getUTCFullYear()
  const month = ('0' + (date.getUTCMonth() + 1)).slice(-2) // 月份是从0开始的
  const day = ('0' + date.getUTCDate()).slice(-2)
  const hours = ('0' + date.getUTCHours()).slice(-2)
  const minutes = ('0' + date.getUTCMinutes()).slice(-2)
  const seconds = ('0' + date.getUTCSeconds()).slice(-2)
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
